const express = require('express');
const router = express.Router();
const Article = require('../models/acticle');
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/spiderProtocol', (req, res, next) => {
  res.json(
    {
      code: 0,
      protocol: {
        name: 'FULL_NET_SPIDER_PROCOL',
        version: '0.1'
      },
      config: {
        contentList: {
          url: 'http://localhost:3000/content',
          pageSizeLimit: 20,
          frequencyLimit: 5
        }
      }
    }
  );
});
router.get('/content', (req, res, next) => {

  
  (async ()=>{
    const { pageSize, lastestId } = req.query;
  
    const match =  {};
    if(lastestId) {match._id = {
      $gt: lastestId
    }};
    console.log(11111);
    const article = await Article.model.find(match).sort({
      _id: 1
    })
      .limit(Number(pageSize) || 10);
    // console.log(article);

    const contentList = [];
    for (let a of article) {
      contentList.push({
        title: a.title,
        contentType: 'dom', //link , full-text,dom,video,redio
        content: {
          html: a.articleContentHtml
        },
        tags: a.tags,
        contentId: a._id,
        source: 'acfun'
      })
    }
    return contentList;
  })().then(r => {
      res.json({
        code:0,
        data:{
          content:r
        }
      })
  }).catch(e => {
      res.json(e)
  })
})

module.exports = router;
